package com.youdoneed.friends.livechat;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.OkHttpUtils;
import com.youdoneed.friends.common.config.HuanXinConfig;
import com.youdoneed.friends.common.util.HuanXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shum
 * @since 2017-06-06
 **/
@RestController
@RequestMapping(value = "/user")
@Api(value = "聊天模块环信用户操作控制器", description = "聊天模块环信用户操作控制器")
public class FriendsUserController {
    private static final Logger logger = LoggerFactory.getLogger(FriendsUserController.class);

    @RequestMapping(value = "/singleRegister", method = {RequestMethod.POST})
    @ApiOperation(notes = "注册IM用户[单个]", value = "注册IM用户[单个]")
    public ResultMessage<String> singleRegister(
            @ApiParam("用户名") @RequestParam String username,
            @ApiParam("昵称") @RequestParam(required = false) String nickname,
            @ApiParam("密码") @RequestParam String password) {
        String apiUrl = HuanXinConfig.url + "users";
        try {
            String token = HuanXinUtil.getAccessToken();
            String result = OkHttpUtils.bodyPost(apiUrl)
                    .header("Authorization", "Bearer " + token)
                    .param("username", username)
                    .param("nickname", nickname)
                    .param("password", password)
                    .callForString();
            logger.info("注册IM用户结果：" + result);
//            if (result == null || result.contains("error")){
//                return new ResultMessage<String>().toFailure("注册IM用户失败！");
//            }
            return new ResultMessage<>(result);
        } catch (Exception e) {
            logger.error("注册IM用户失败：" + e);
            return new ResultMessage<String>().toFailure("注册IM用户失败！");
        }
    }

    @RequestMapping(value = "/getSingleUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "获取IM用户[单个]", value = "获取IM用户[单个]")
    public ResultMessage<String> getSingleUser(@ApiParam("用户名") @RequestParam String username) {
        String apiUrl = HuanXinConfig.url + "users/" + username;
        try {
            String token = HuanXinUtil.getAccessToken();
            String result = OkHttpUtils.get(apiUrl)
                    .header("Authorization", "Bearer " + token)
                    .callForString();
            return new ResultMessage<>(result);
        } catch (Exception e) {
            logger.error("获取IM用户失败：" + e);
            return new ResultMessage<String>().toFailure("获取IM用户失败！");
        }
    }

    @RequestMapping(value = "/updateNickName", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "修改用户昵称", value = "修改用户昵称")
    public ResultMessage<String> updateNickName(@ApiParam("用户名") @RequestParam String username,
                                                @ApiParam("昵称") @RequestParam String nickname) {
        String apiUrl = HuanXinConfig.url + "users/" + username;
        try {
            String token = HuanXinUtil.getAccessToken();
            String result = OkHttpUtils.bodyPut(apiUrl)
                    .header("Authorization", "Bearer " + token)
                    .param("nickname", nickname)
                    .callForString();
            return new ResultMessage<>(result);
        } catch (Exception e) {
            logger.error("修改用户昵称失败：" + e);
            return new ResultMessage<String>().toFailure("修改用户昵称失败！");
        }
    }

    @RequestMapping(value = "/deleteSingleUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "删除单个IM用户", value = "删除单个IM用户")
    public ResultMessage<String> deleteSingleUser(@ApiParam("用户名") @RequestParam String username) {
        String apiUrl = HuanXinConfig.url + "users/" + username;
        try {
            String token = HuanXinUtil.getAccessToken();
            String result = OkHttpUtils.delete(apiUrl)
                    .header("Authorization", "Bearer " + token)
                    .callForString();
            return new ResultMessage<>(result);
        } catch (Exception e) {
            logger.error("删除单个IM用户失败：" + e);
            return new ResultMessage<String>().toFailure("删除单个IM用户失败！");
        }
    }

    @RequestMapping(value = "/addUserFriend", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "给IM用户添加好友", value = "给IM用户添加好友")
    public ResultMessage<String> addUserFriend(@ApiParam("我的用户名") @RequestParam String owner_username,
                                               @ApiParam("好友用户名") @RequestParam String friend_username) {
        String apiUrl = HuanXinConfig.url + "users/" + owner_username + "/contacts/users/" + friend_username;
        try {
            String token = HuanXinUtil.getAccessToken();
            String result = OkHttpUtils.bodyPost(apiUrl)
                    .header("Authorization", "Bearer " + token)
                    .callForString();
            return new ResultMessage<>(result);
        } catch (Exception e) {
            logger.error("给IM用户添加好友失败：" + e);
            return new ResultMessage<String>().toFailure("给IM用户添加好友失败！");
        }
    }

    @RequestMapping(value = "/checkUserFriend", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查看IM好友", value = "查看IM好友")
    public ResultMessage<String> checkUserFriend(@ApiParam("我的用户名") @RequestParam String owner_username) {
        String apiUrl = HuanXinConfig.url + "users/" + owner_username;
        try {
            String token = HuanXinUtil.getAccessToken();
            String result = OkHttpUtils.get(apiUrl)
                    .header("Authorization", "Bearer " + token)
                    .callForString();
            return new ResultMessage<>(result);
        } catch (Exception e) {
            logger.error("查看好友失败：" + e);
            return new ResultMessage<String>().toFailure("查看好友失败！");
        }
    }

}
